import request from "../utils/request";
import { User, UserInfoType } from "../types/user";
import { CartProductType } from "../types/prod";
// 注册
export const userReg = (user: User) => {
  return request.post("users", user);
};

//登录
export const userLogin = (user: User) => {
  return request.post("login", user);
};

//更新用户
interface UpdateInfoType {
  avatar: string;
}
export const userUpdate = (
  userid: string,
  token: string,
  updateObj: UpdateInfoType
) => {
  return request.put(`users/${userid}`, updateObj, {
    headers: {
      "X-LC-Session": token, //不携带token，后端拒绝更新用户表
    },
  });
};

// 新增购物车
export const cartPost = (cartObj: CartProductType) => {
  return request.post("classes/VueCart", cartObj);
};

// 更新购物车（数量、勾选）
interface CartUpdateType {
  ischeck?: boolean;
  num?: number;
}
export const cartPut = (cartid: string, updateObj: CartUpdateType) => {
  return request.put(`classes/VueCart/${cartid}`, updateObj);
};

//删除购物车
export const cartDel = (cartid: string) => {
  return request.delete(`classes/VueCart/${cartid}`);
};

//请求用户购物车数据
export const cartGet = (userid: string) => {
  return request.get(`classes/VueCart`, {
    params: {
      where: `{
        "userid":"${userid}"
      }`,
    },
  });

  // return request.get(`classes/VueCart?where={"userid":"${userid}"}`);
};
